package com.yyfax.ph.progress.checkCredit;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.yyfax.ph.Global;
import com.yyfax.ph.R;
import com.yyfax.ph.base.YJPH;
import com.yyfax.ph.base.adapter.BaseAdapter;
import com.yyfax.ph.common.imageselector.activity.PhotoViewActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by laixj on 2017/4/6.
 * Copyright ©2016 www.yylending.com. All Rights Reserved.
 */
public class CreditImageAdapter extends BaseAdapter<CheckCreditModel.Credit> {


	private final ArrayList<String> mPicUrls;
	private final HashMap<String, String> mOption;

	public CreditImageAdapter(List<CheckCreditModel.Credit> credits, HashMap<String, String> option) {
		super(credits);
		mPicUrls = getPicUrls();
		mOption = option;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ViewHolder viewHolder;
		final Context context = parent.getContext();
		if (convertView == null) {
			convertView = View.inflate(context, R.layout.module_image_item, null);
			viewHolder = new ViewHolder(convertView);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		String name;
		if (null == mOption) {//有通道征信
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.append(context.getString(R.string.credit_report)).append("(").append(position + 1).append(")");
			name = stringBuilder.toString();
		} else {//无通道征信
			name = mOption.get(getItem(position).type);
		}
		viewHolder.nameTv.setText(name);
		YJPH.load(context, mPicUrls.get(position), viewHolder.showImage, R.drawable.loading_image_credit, R.drawable.loading_image_error);
		viewHolder.showImage.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				PhotoViewActivity.start((Activity) context, mPicUrls, position, false);
			}
		});

		return convertView;
	}

	static class ViewHolder {
		@BindView(R.id.show_image)
		ImageView showImage;
		@BindView(R.id.name_tv)
		TextView nameTv;

		ViewHolder(View view) {
			ButterKnife.bind(this, view);
		}
	}

	private ArrayList<String> getPicUrls() {
		ArrayList<String> picUrls = new ArrayList();
		for (CheckCreditModel.Credit credit : getDataSet()) {
			StringBuffer url = new StringBuffer();
			url.append(Global.BASE_URL).append(credit.url);
			picUrls.add(url.toString());
		}
		return picUrls;

	}

}